﻿from Framework.PageClasses._CpBase import _CpBase
from Framework.PageClasses.CpAuction import CpAuction
from Framework.PageClasses.CpHome import CpHome
from Framework.PageClasses.CpDefault import CpDefault
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import time

class CpLogin(CpHome, _CpBase):
    """Класс окна авторизации"""
    url= 'https://etp:varenduTest@www.etp.edinews.ru/authentication/login/'
    #url= 'http://etp.edinews.ru'

    #Установить имя пользователя
    def SetName(self, sUser):
        self.driver.find_element_by_id('login').send_keys(sUser)
        return self

    #Установить пароль
    def SetPassword(self, sPass):
        self.driver.find_element_by_id('password').send_keys(sPass)
        return self

    #TODO: Переделать на CSS
    #Войти
    def Enter(self):
        self.driver.find_element_by_xpath(".//*[@type='submit' and contains(.,'Вход в систему')]").click()
        #return CpAuction(self.driver)

    #TODO: Переделать на CSS
    #Войти по ЭЦП
    def EnterEDS(self):
        self.driver.find_element_by_xpath(".//*[@type='submit' and contains(.,'Вход в систему')]").click()
        return CpAuction(self.driver)
        
    #Привязать сессию
    def BindSession(self, bValue):
        pass

    #Востановить пароль
    def RestorePassword(self):
        pass

    #Регистрация
    def Registration(self):
        pass

    def select_cryptopro_sign(self):
        self.driver.find_element_by_xpath(".//*[@type='button' and text()='Выбор плагина ЭЦП']").click()
        self.driver.execute_script("document.getElementsByName('selected_plugin')[1].style.visibility = 'visible';")
        self.driver.execute_script("document.getElementsByName('selected_plugin')[1].style.top = 0;")
        self.driver.execute_script("document.getElementsByName('selected_plugin')[1].style.left = 0;")
        #self.driver.find_element_by_css_selector("input[value='cryptopro']").click().perform()
        #self.driver.find_element_by_css_selector("input[value='cryptopro']").send_keys(Keys.ENTER)
        el = self.driver.find_element_by_css_selector("input[value='cryptopro']")
        webdriver.ActionChains(self.driver).move_to_element(el).click(el).perform()


        self.driver.find_element_by_xpath(".//*[@type='button' and text()='Выбрать']").click()
        self.driver.find_element_by_xpath(".//*[@type='button' and text()='OK']").click()

    def loginAs(self, user):
        self.SetName(user.name).SetPassword(user.password)
        #self.select_cryptopro_sign()
        self.Enter()
        return CpDefault(self.driver)


